package com.basker.pisces.tools.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.basker.pisces.core.query.meta.QueryMeta;
import com.basker.pisces.core.serialize.xml.DataContractXmlSerializer;
import com.basker.pisces.domain.entity.meta.Entity;
import com.basker.pisces.service.annotation.IgnoreLoginContext;
import com.basker.pisces.spring.SpringApplicationContext;
import com.basker.pisces.tools.diagnosis.EntityDiagnoser;

@ConditionalOnClass(Entity.class)
@RestController
@RequestMapping("/t/entity")
public class EntityRestController {

    @Autowired
    private BeanFactory beanFactory;

    @Autowired
    private SpringApplicationContext springApplicationContext;

    @Autowired
    private EntityDiagnoser entityDiagnoser;

    @GetMapping(value = "/diagnose/displayname/all")
    @IgnoreLoginContext
    public JSONObject diagnoseDisplayname() {
        List<Entity> beans = this.springApplicationContext.getBeans(Entity.class);

        JSONObject ret = new JSONObject();

        for (Entity entity : beans) {
            Map<String, Object> desc = this.entityDiagnoser.diagnoseElementDisplayName(entity);
            ret.put(entity.getName(), desc);
        }

        return ret;
    }

    @GetMapping(value = "/diagnose/displayname/{entityName}")
    @IgnoreLoginContext
    public JSONObject diagnoseDisplayname(@PathVariable("entityName") String entityName) {
        entityName = StringUtils.uncapitalize(entityName);
        Entity entity = this.beanFactory.getBean(entityName, Entity.class);

        Map<String, Object> desc = entityDiagnoser.diagnoseElementDisplayName(entity);
        return new JSONObject(desc);

    }

    @GetMapping(value = "/meta/{entityName}", produces = "application/xml;charset=UTF-8")
    @IgnoreLoginContext
    public String meta(@PathVariable("entityName") String entityName) {
        entityName = StringUtils.uncapitalize(entityName);
        Entity entity = this.beanFactory.getBean(entityName, Entity.class);

        DataContractXmlSerializer serializer = new DataContractXmlSerializer(false);
        String xml = serializer.serializeToString(entity, false, false);

        return xml;
    }

    @GetMapping(value = "/querymeta/{entityName}")
    @IgnoreLoginContext
    public JSONObject queryMeta(@PathVariable("entityName") String entityName) {
        entityName = StringUtils.uncapitalize(entityName);
        Entity entity = this.beanFactory.getBean(entityName, Entity.class);

        QueryMeta queryMeta = entity.getQueryMeta();
        String json = JSON.toJSONString(queryMeta);

        return JSONObject.parseObject(json);
    }
}
